package com.aiyige.page.user.adapter;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.db.dao.InfoModelDao;
import com.aiyige.base.db.dao.NormalVideoModelDao;
import com.aiyige.base.db.dao.PhotoModelDao;
import com.aiyige.base.db.dao.UploadFileDao;
import com.aiyige.base.db.table.UploadFile;
import com.aiyige.base.eventbus.EventPublishProgressStatusUpdate;
import com.aiyige.base.eventbus.EventUploadUpdate;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.publish.photo.model.PublishPhotoItem;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.page.user.model.DynamicMoment;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.CenteredImageSpan;
import com.aiyige.utils.DateFormatUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.WebImageUrlModifier;
import com.aiyige.utils.widget.LabelProgressBar;
import com.aiyige.utils.widget.RoundCornerImageView;
import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * Created by zhuyakun on 2017/10/19.
 */

public class DynamicAdapter extends BaseQuickAdapter<DynamicMoment, BaseViewHolder> {
    EventBus eventBus;

    public DynamicAdapter() {
        super(new LinkedList<DynamicMoment>());
        eventBus = EventBus.builder().build();
    }

    public EventBus getEventBus() {
        return eventBus;
    }

    @Override
    public void onViewAttachedToWindow(BaseViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        try {
            eventBus.register(holder);
        } catch (Exception e) {
            Timber.e("onViewAttachedToWindow:" + Log.getStackTraceString(e));
        }
    }

    @Override
    public void onViewDetachedFromWindow(BaseViewHolder holder) {
        try {
            eventBus.unregister(holder);
        } catch (Exception e) {

        }
        super.onViewDetachedFromWindow(holder);
    }

    @Override
    protected void convert(BaseViewHolder helper, DynamicMoment item) {
        switch (item.getSubjectIndex()) {
            case Moment.SUBJECT_PHOTO_INDEX:
                ((PhotoViewHolder) helper).bindData(item);
                break;
            case Moment.SUBJECT_VIDEO_INDEX:
                ((VideoViewHolder) helper).bindData(item);
                break;
            case Moment.SUBJECT_ARTICLE_INDEX:
                ((InfoViewHolder) helper).bindData(item);
                break;
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).getSubjectIndex();
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case Moment.SUBJECT_PHOTO_INDEX:
                baseViewHolder = new PhotoViewHolder(getItemView(R.layout.dynamic_photo_item, parent));
                break;
            case Moment.SUBJECT_VIDEO_INDEX:
                baseViewHolder = new VideoViewHolder(getItemView(R.layout.dynamic_video_item, parent));
                break;
            case Moment.SUBJECT_ARTICLE_INDEX:
                baseViewHolder = new InfoViewHolder(getItemView(R.layout.dynamic_info_item, parent));
                break;
            default:
                baseViewHolder = new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
                break;
        }
        return baseViewHolder;
    }

    public class AdViewHolder extends BaseViewHolder {
        DynamicMoment dynamicMoment;
        @BindView(R.id.adIv)
        RoundCornerImageView adIv;

        public AdViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(DynamicMoment data) {
            dynamicMoment = data;
            Glide.with(itemView.getContext())
                    .load(ListUtil.isEmpty(data.getCover()) ? "" : data.getCover().get(0))
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(adIv);
        }

        @OnClick({R.id.adIv, R.id.containerLayout})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.adIv:
                    break;
                case R.id.containerLayout:
                    break;
            }
        }
    }


    public class InfoViewHolder extends BaseViewHolder {
        @BindView(R.id.releaseDateTv)
        TextView releaseDateTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.likeIv)
        ImageView likeIv;
        @BindView(R.id.likeTv)
        TextView likeTv;
        @BindView(R.id.commentIv)
        ImageView commentIv;
        @BindView(R.id.commentTv)
        TextView commentTv;
        @BindView(R.id.collectionIv)
        ImageView collectionIv;
        @BindView(R.id.collectionTv)
        TextView collectionTv;
        @BindView(R.id.statusLayout)
        View statusLayout;
        @BindView(R.id.statusIv)
        ImageView statusIv;
        @BindView(R.id.statusTv)
        TextView statusTv;
        @BindView(R.id.countLayout)
        View countLayout;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;
        @BindView(R.id.labelProgressBar)
        LabelProgressBar labelProgressBar;
        @BindView(R.id.moreBtn)
        ImageView moreBtn;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.userNameTv)
        TextView userNameTv;
        DynamicMoment records;


        public InfoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.statusIv);
            addOnClickListener(R.id.moreBtn);
        }


        public void bindData(DynamicMoment data) {
            records = data;
            refreshLocalData();
            if (data.infoModel == null) {
                Glide.with(itemView.getContext())
                        .load(data.getCreatorBackup().getAvatar())
                        .apply(RequestOptions.circleCropTransform())
                        .into(avatarIv);
                userNameTv.setText(data.getCreatorBackup().getName());
                moreBtn.setVisibility(data.isEditable() ? View.VISIBLE : View.INVISIBLE);
                countLayout.setVisibility(View.VISIBLE);
                statusLayout.setVisibility(View.INVISIBLE);
                Glide.with(itemView.getContext())
                        .load(WebImageUrlModifier.bigImage(ListUtil.isEmpty(records.getCover()) ? "" : records.getCover().get(0)))
                        .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                        .into(coverIv);
                ImageSpan articleImageLabel = new CenteredImageSpan(itemView.getContext(), R.drawable.home_article);
                SpannableString spannableString = new SpannableString("  " + records.getTitle());
                spannableString.setSpan(articleImageLabel, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                titleTv.setText(spannableString);
                likeTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getPraiseCount()));
                collectionTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getFavoriteCount()));
                commentTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getCommentCount()));
                watchNumTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getViewCount()));
                releaseDateTv.setText(DateFormatUtil.getStandardDate(records.getOnsellTime()));
                releaseDateTv.setVisibility(View.VISIBLE);
            } else {
                if (data.infoModel.getControlOption() == PublishUtil.CONTROL_OPTION_DELETE) {
                    remove(getAdapterPosition());
                    return;
                }
                Glide.with(itemView.getContext())
                        .load(AccountUtil.getCurrentUser().getAvatar())
                        .apply(RequestOptions.circleCropTransform())
                        .into(avatarIv);
                userNameTv.setText(AccountUtil.getCurrentUser().getAccount());
                moreBtn.setVisibility(View.VISIBLE);
                Glide.with(itemView.getContext())
                        .load(data.infoModel.getCoverUrl())
                        .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                        .into(coverIv);
                releaseDateTv.setText(DateFormatUtil.getStandardDate(data.infoModel.getSellDate()));
                likeTv.setText(StringUtils.formatNum(0));
                collectionTv.setText(StringUtils.formatNum(0));
                commentTv.setText(StringUtils.formatNum(0));
                watchNumTv.setText(StringUtils.formatNum(0));
                titleTv.setText(data.infoModel.getTitle());
                switch (data.infoModel.getProgressStatus()) {
                    case PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE:
                    case PublishUtil.PROGRESS_STATUS_TRANSCODE_FINISH:
                    case PublishUtil.PROGRESS_STATUS_TRANSCODE_FAILED:
                    case PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD:
                    case PublishUtil.PROGRESS_STATUS_UPLOADING:
                    case PublishUtil.PROGRESS_STATUS_UPLOAD_FINISH:
                    case PublishUtil.PROGRESS_STATUS_SUBMITTING:
                    case PublishUtil.PROGRESS_STATUS_SUBMIT_FAILED:
                        releaseDateTv.setVisibility(View.INVISIBLE);
                        statusLayout.setVisibility(View.VISIBLE);
                        countLayout.setVisibility(View.GONE);
                        break;
                    default:
                        releaseDateTv.setVisibility(View.VISIBLE);
                        countLayout.setVisibility(View.VISIBLE);
                        statusLayout.setVisibility(View.INVISIBLE);
                        break;
                }
                switch (data.infoModel.getControlOption()) {
                    case PublishUtil.CONTROL_OPTION_UPLOAD:
                        statusIv.setImageResource(R.drawable.upload);
                        statusTv.setText(R.string.uploading);
                        break;
                    case PublishUtil.CONTROL_OPTION_PAUSE:
                        statusIv.setImageResource(R.drawable.suspend);
                        statusTv.setText(R.string.pausing);
                        break;
                    case PublishUtil.CONTROL_OPTION_FAILED_PAUSE:
                        statusIv.setImageResource(R.drawable.suspend);
                        statusTv.setText(R.string.upload_failed_retry);
                        break;
                }
                Timber.e("bindData:progerss=" + data.infoModel.getUploadedPercent() * 100);
                labelProgressBar.setProgress(data.infoModel.getUploadedPercent() * 100);
            }
        }

        @OnClick({R.id.containerLayout})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.containerLayout:
                    break;
            }
        }

        private void refreshLocalData() {
            if (records.infoModel != null) {
                try {
                    InfoModelDao.getDao().refresh(records.infoModel);
                    List<UploadFile> uploadFileList = UploadFileDao.getDao().queryBuilder()
                            .where()
                            .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE)
                            .and()
                            .eq(UploadFile.PARENT_ID_FIELD, records.infoModel.getId())
                            .query();
                    long uploadedSize = 0;
                    long totalSize = 0;
                    for (UploadFile uploadFile : uploadFileList) {
                        uploadedSize += uploadFile.getUploadedSize();
                        totalSize += uploadFile.getTotalSize();
                    }
                    records.infoModel.setUploadedPercent(((float) uploadedSize) / totalSize);
                } catch (SQLException e) {
                    Timber.e("onEventPublishProgressStatusUpdate:" + Log.getStackTraceString(e));
                }
            }
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onEventPublishProgressStatusUpdate(EventPublishProgressStatusUpdate event) {
            if (records.infoModel != null) {
                bindData(records);
            }
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onEventUploadUpdate(EventUploadUpdate event) {
            if (records.infoModel != null) {
                bindData(records);
            }
        }
    }

    public class PhotoViewHolder extends BaseViewHolder {
        DynamicMoment records;
        @BindView(R.id.releaseDateTv)
        TextView releaseDateTv;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.likeIv)
        ImageView likeIv;
        @BindView(R.id.likeTv)
        TextView likeTv;
        @BindView(R.id.commentIv)
        ImageView commentIv;
        @BindView(R.id.commentTv)
        TextView commentTv;
        @BindView(R.id.collectionIv)
        ImageView collectionIv;
        @BindView(R.id.collectionTv)
        TextView collectionTv;

        @BindView(R.id.onePhotoLayout)
        View onePhotoLayout;

        @BindView(R.id.twoPhotoLayout)
        View twoPhotoLayout;

        @BindView(R.id.threePhotoLayout)
        View threePhotoLayout;

        @BindView(R.id.onePhotoIv)
        ImageView onePhotoIv;

        @BindView(R.id.twoPhotoIv0)
        ImageView twoPhotoIv0;

        @BindView(R.id.twoPhotoIv1)
        ImageView twoPhotoIv1;

        @BindView(R.id.threePhotoIv0)
        ImageView threePhotoIv0;

        @BindView(R.id.threePhotoIv1)
        ImageView threePhotoIv1;

        @BindView(R.id.threePhotoIv2)
        ImageView threePhotoIv2;

        @BindView(R.id.statusLayout)
        View statusLayout;
        @BindView(R.id.statusIv)
        ImageView statusIv;
        @BindView(R.id.statusTv)
        TextView statusTv;
        @BindView(R.id.countLayout)
        View countLayout;
        @BindView(R.id.moreBtn)
        ImageView moreBtn;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.userNameTv)
        TextView userNameTv;

        public PhotoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.statusIv);
            addOnClickListener(R.id.moreBtn);
        }


        public void bindData(DynamicMoment data) {
            records = data;
            if (records.photoModel != null) {
                try {
                    PhotoModelDao.getDao().refresh(records.photoModel);
                } catch (Exception e) {
                    Timber.e("onEvent:" + Log.getStackTraceString(e));
                }
                Glide.with(itemView.getContext())
                        .load(AccountUtil.getCurrentUser().getAvatar())
                        .apply(RequestOptions.circleCropTransform())
                        .into(avatarIv);
                userNameTv.setText(AccountUtil.getCurrentUser().getAccount());
            } else {
                Glide.with(itemView.getContext())
                        .load(data.getCreatorBackup().getAvatar())
                        .apply(RequestOptions.circleCropTransform())
                        .into(avatarIv);
                userNameTv.setText(data.getCreatorBackup().getName());
            }
            if (data.photoModel == null) {
                moreBtn.setVisibility(data.isEditable() ? View.VISIBLE : View.INVISIBLE);
                countLayout.setVisibility(View.VISIBLE);
                statusLayout.setVisibility(View.INVISIBLE);
                int showCoverNum = 0;
                if (records.getCover() != null) {
                    showCoverNum = records.getCover().size() > 3 ? 3 : records.getCover().size();
                }
                switch (showCoverNum) {
                    case 3: {
                        onePhotoLayout.setVisibility(View.INVISIBLE);
                        twoPhotoLayout.setVisibility(View.INVISIBLE);
                        threePhotoLayout.setVisibility(View.VISIBLE);
                        Glide.with(itemView.getContext())
                                .load(WebImageUrlModifier.bigImage(records.getCover().get(0)))
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(threePhotoIv0);
                        Glide.with(itemView.getContext())
                                .load(WebImageUrlModifier.bigImage(records.getCover().get(1)))
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(threePhotoIv1);
                        Glide.with(itemView.getContext())
                                .load(WebImageUrlModifier.bigImage(records.getCover().get(2)))
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(threePhotoIv2);
                        break;
                    }
                    case 2: {
                        onePhotoLayout.setVisibility(View.INVISIBLE);
                        twoPhotoLayout.setVisibility(View.VISIBLE);
                        threePhotoLayout.setVisibility(View.INVISIBLE);
                        Glide.with(itemView.getContext())
                                .load(WebImageUrlModifier.bigImage(records.getCover().get(0)))
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(twoPhotoIv0);
                        Glide.with(itemView.getContext())
                                .load(WebImageUrlModifier.bigImage(records.getCover().get(1)))
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(twoPhotoIv1);
                        break;
                    }
                    case 1: {
                        onePhotoLayout.setVisibility(View.VISIBLE);
                        twoPhotoLayout.setVisibility(View.INVISIBLE);
                        threePhotoLayout.setVisibility(View.INVISIBLE);
                        Glide.with(itemView.getContext())
                                .load(WebImageUrlModifier.bigImage(records.getCover().get(0)))
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(onePhotoIv);
                        break;
                    }
                    case 0: {
                        onePhotoLayout.setVisibility(View.INVISIBLE);
                        twoPhotoLayout.setVisibility(View.INVISIBLE);
                        threePhotoLayout.setVisibility(View.INVISIBLE);
                        break;
                    }
                }
                releaseDateTv.setText(DateFormatUtil.getStandardDate(records.getOnsellTime()));
                releaseDateTv.setVisibility(View.VISIBLE);
                watchNumTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getViewCount()));
                titleTv.setText(records.getTitle());
                likeTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getPraiseCount()));
                collectionTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getFavoriteCount()));
                commentTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getCommentCount()));
            } else {
                if (data.photoModel.getControlOption() == PublishUtil.CONTROL_OPTION_DELETE) {
                    remove(getAdapterPosition());
                    return;
                }
                moreBtn.setVisibility(View.VISIBLE);
                int showCoverNum = 0;
                PublishPhotoItem[] publishPhotoItemArray = JSON.parseObject(data.photoModel.getCover(), PublishPhotoItem[].class);
                if (publishPhotoItemArray != null) {
                    showCoverNum = publishPhotoItemArray.length > 3 ? 3 : publishPhotoItemArray.length;
                }
                switch (showCoverNum) {
                    case 3: {
                        onePhotoLayout.setVisibility(View.INVISIBLE);
                        twoPhotoLayout.setVisibility(View.INVISIBLE);
                        threePhotoLayout.setVisibility(View.VISIBLE);
                        Glide.with(itemView.getContext())
                                .load(publishPhotoItemArray[0].getPhotoUrl())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(threePhotoIv0);
                        Glide.with(itemView.getContext())
                                .load(publishPhotoItemArray[1].getPhotoUrl())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(threePhotoIv1);
                        Glide.with(itemView.getContext())
                                .load(publishPhotoItemArray[2].getPhotoUrl())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(threePhotoIv2);
                        break;
                    }
                    case 2: {
                        onePhotoLayout.setVisibility(View.INVISIBLE);
                        twoPhotoLayout.setVisibility(View.VISIBLE);
                        threePhotoLayout.setVisibility(View.INVISIBLE);
                        Glide.with(itemView.getContext())
                                .load(publishPhotoItemArray[0].getPhotoUrl())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(twoPhotoIv0);
                        Glide.with(itemView.getContext())
                                .load(publishPhotoItemArray[1].getPhotoUrl())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(twoPhotoIv1);
                        break;
                    }
                    case 1: {
                        onePhotoLayout.setVisibility(View.VISIBLE);
                        twoPhotoLayout.setVisibility(View.INVISIBLE);
                        threePhotoLayout.setVisibility(View.INVISIBLE);
                        Glide.with(itemView.getContext())
                                .load(publishPhotoItemArray[0].getPhotoUrl())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                                .into(onePhotoIv);
                        break;
                    }
                    case 0: {
                        onePhotoLayout.setVisibility(View.INVISIBLE);
                        twoPhotoLayout.setVisibility(View.INVISIBLE);
                        threePhotoLayout.setVisibility(View.INVISIBLE);
                        break;
                    }
                }

                releaseDateTv.setText(DateFormatUtil.getStandardDate(data.photoModel.getSellDate()));
                titleTv.setText(records.photoModel.getTitle());
                watchNumTv.setText(StringUtils.formatNum(0));
                likeTv.setText(StringUtils.formatNum(0));
                collectionTv.setText(StringUtils.formatNum(0));
                commentTv.setText(StringUtils.formatNum(0));
                switch (data.photoModel.getProgressStatus()) {
                    case PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE:
                    case PublishUtil.PROGRESS_STATUS_TRANSCODE_FINISH:
                    case PublishUtil.PROGRESS_STATUS_TRANSCODE_FAILED:
                    case PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD:
                    case PublishUtil.PROGRESS_STATUS_UPLOADING:
                    case PublishUtil.PROGRESS_STATUS_UPLOAD_FINISH:
                    case PublishUtil.PROGRESS_STATUS_SUBMITTING:
                    case PublishUtil.PROGRESS_STATUS_SUBMIT_FAILED:
                        statusLayout.setVisibility(View.VISIBLE);
                        countLayout.setVisibility(View.GONE);
                        releaseDateTv.setVisibility(View.INVISIBLE);
                        break;
                    default:
                        countLayout.setVisibility(View.VISIBLE);
                        statusLayout.setVisibility(View.INVISIBLE);
                        releaseDateTv.setVisibility(View.VISIBLE);
                        break;
                }
                switch (data.photoModel.getControlOption()) {
                    case PublishUtil.CONTROL_OPTION_UPLOAD:
                        statusIv.setImageResource(R.drawable.upload);
                        statusTv.setText(R.string.uploading);
                        break;
                    case PublishUtil.CONTROL_OPTION_PAUSE:
                        statusIv.setImageResource(R.drawable.suspend);
                        statusTv.setText(R.string.pausing);
                        break;
                    case PublishUtil.CONTROL_OPTION_FAILED_PAUSE:
                        statusIv.setImageResource(R.drawable.suspend);
                        statusTv.setText(R.string.upload_failed_retry);
                        break;
                }
            }
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onEventPublishProgressStatusUpdate(EventPublishProgressStatusUpdate event) {
            if (records.photoModel != null) {
                bindData(records);
            }
        }


        @OnClick({R.id.containerLayout})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.containerLayout:
                    break;
            }
        }

    }

    public class VideoViewHolder extends BaseViewHolder {
        DynamicMoment records;
        @BindView(R.id.releaseDateTv)
        TextView releaseDateTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.likeIv)
        ImageView likeIv;
        @BindView(R.id.likeTv)
        TextView likeTv;
        @BindView(R.id.commentIv)
        ImageView commentIv;
        @BindView(R.id.commentTv)
        TextView commentTv;
        @BindView(R.id.collectionIv)
        ImageView collectionIv;
        @BindView(R.id.collectionTv)
        TextView collectionTv;
        @BindView(R.id.statusLayout)
        View statusLayout;
        @BindView(R.id.statusIv)
        ImageView statusIv;
        @BindView(R.id.statusTv)
        TextView statusTv;
        @BindView(R.id.countLayout)
        View countLayout;
        @BindView(R.id.moreBtn)
        ImageView moreBtn;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.userNameTv)
        TextView userNameTv;

        public VideoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.statusIv);
            addOnClickListener(R.id.moreBtn);
        }


        public void bindData(DynamicMoment data) {
            records = data;
            if (records.normalVideoModel != null) {
                try {
                    NormalVideoModelDao.getDao().refresh(records.normalVideoModel);
                } catch (Exception e) {
                    Timber.e("onEvent:" + Log.getStackTraceString(e));
                }
                Glide.with(itemView.getContext())
                        .load(AccountUtil.getCurrentUser().getAvatar())
                        .apply(RequestOptions.circleCropTransform())
                        .into(avatarIv);
                userNameTv.setText(AccountUtil.getCurrentUser().getAccount());
            } else {
                Glide.with(itemView.getContext())
                        .load(data.getCreatorBackup().getAvatar())
                        .apply(RequestOptions.circleCropTransform())
                        .into(avatarIv);
                userNameTv.setText(data.getCreatorBackup().getName());
            }
            if (data.normalVideoModel == null) {
                moreBtn.setVisibility(data.isEditable() ? View.VISIBLE : View.INVISIBLE);
                Glide.with(itemView.getContext())
                        .load(WebImageUrlModifier.bigImage(ListUtil.isEmpty(records.getCover()) ? "" : records.getCover().get(0)))
                        .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                        .into(coverIv);
                watchNumTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getViewCount()));
                titleTv.setText(records.getTitle());
                likeTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getPraiseCount()));
                collectionTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getFavoriteCount()));
                commentTv.setText(StringUtils.formatNum(records.getStatisticsBackup().getCommentCount()));
                releaseDateTv.setText(DateFormatUtil.getStandardDate(records.getOnsellTime()));
                releaseDateTv.setVisibility(View.VISIBLE);
                countLayout.setVisibility(View.VISIBLE);
                statusLayout.setVisibility(View.INVISIBLE);
            } else {
                if (data.normalVideoModel.getControlOption() == PublishUtil.CONTROL_OPTION_DELETE) {
                    remove(getAdapterPosition());
                    return;
                }
                moreBtn.setVisibility(View.VISIBLE);
                Glide.with(itemView.getContext())
                        .load(data.normalVideoModel.getCoverUrl())
                        .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                        .into(coverIv);
                titleTv.setText(data.normalVideoModel.getTitle());
                watchNumTv.setText(StringUtils.formatNum(0));
                likeTv.setText(StringUtils.formatNum(0));
                collectionTv.setText(StringUtils.formatNum(0));
                commentTv.setText(StringUtils.formatNum(0));
                releaseDateTv.setText(DateFormatUtil.getStandardDate(data.normalVideoModel.getSellDate()));
                switch (data.normalVideoModel.getProgressStatus()) {
                    case PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE:
                    case PublishUtil.PROGRESS_STATUS_TRANSCODE_FINISH:
                    case PublishUtil.PROGRESS_STATUS_TRANSCODE_FAILED:
                    case PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD:
                    case PublishUtil.PROGRESS_STATUS_UPLOADING:
                    case PublishUtil.PROGRESS_STATUS_UPLOAD_FINISH:
                    case PublishUtil.PROGRESS_STATUS_SUBMITTING:
                    case PublishUtil.PROGRESS_STATUS_SUBMIT_FAILED:
                        statusLayout.setVisibility(View.VISIBLE);
                        countLayout.setVisibility(View.GONE);
                        releaseDateTv.setVisibility(View.INVISIBLE);
                        break;
                    default:
                        countLayout.setVisibility(View.VISIBLE);
                        statusLayout.setVisibility(View.INVISIBLE);
                        releaseDateTv.setVisibility(View.VISIBLE);
                        break;
                }
                switch (data.normalVideoModel.getControlOption()) {
                    case PublishUtil.CONTROL_OPTION_UPLOAD:
                        statusIv.setImageResource(R.drawable.upload);
                        statusTv.setText(R.string.uploading);
                        break;
                    case PublishUtil.CONTROL_OPTION_PAUSE:
                        statusIv.setImageResource(R.drawable.suspend);
                        statusTv.setText(R.string.pausing);
                        break;
                    case PublishUtil.CONTROL_OPTION_FAILED_PAUSE:
                        statusIv.setImageResource(R.drawable.suspend);
                        statusTv.setText(R.string.upload_failed_retry);
                        break;
                }
            }
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onEventPublishProgressStatusUpdate(EventPublishProgressStatusUpdate event) {
            if (records.normalVideoModel != null) {
                bindData(records);
            }
        }

        @OnClick({R.id.containerLayout})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.containerLayout:
                    break;
            }
        }
    }
}
